/*****************************************************************************\

 Javascript xml request library. Based on "SOAP Client" library from Matteo Casati - http://www.guru4.net/
 
 @version: 1.0 - 2008.03.01
 @author: Thomas Andersen - http://www.invulgo.dk
 
\*****************************************************************************/

function XMLReqParameters()
{
	var _pl = new Array();
	this.add = function(name, value) 
	{
		_pl[name] = value; 
		return this; 
	}
	this.toXml = function()
	{
		var xml = "";
		for(var p in _pl)
		{
			switch(typeof(_pl[p])) 
			{
                case "string":
                case "number":
                case "boolean":
                case "object":
                    xml += "<param id=\"" + p + "\">" + XMLReqParameters._serialize(_pl[p]) + "</param>";
                    break;
                default:
                    break;
            }
		}
		return xml;	
	}
}
XMLReqParameters._serialize = function(o)
{
    var s = "";
    switch(typeof(o))
    {
        case "string":
            s += escape(o); break;
        case "number":
        case "boolean":
            s += o.toString(); break;
        default:
            throw new Error(500, "XMLReqParameters: type '" + typeof(o) + "' is not supported");
    }
    return s;
}

function XMLReq() {}

XMLReq.invoke = function(url, method, parameters, async, callback, parm)
{
	if(async)
		XMLReq._sendSoapRequest(url, method, parameters, async, callback, parm);
	else
		return XMLReq._sendSoapRequest(url, method, parameters, async, callback, parm);
}

XMLReq._sendSoapRequest = function(url, method, parameters, async, callback, parm)
{
	// build SOAP request
	var sr = 
				"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
				"<servicecall operation=\"" + method + "\">" + 
				"<parameters>" +
				parameters.toXml() +
				"</parameters></servicecall>";
	// send request
	var xmlHttp = XMLReq._getXmlHttp();
	xmlHttp.open("POST", url, async);
	xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
	if(async) 
	{
		xmlHttp.onreadystatechange = function() 
		{
			if(xmlHttp.readyState == 4)
				XMLReq._onSendSoapRequest(method, async, callback, xmlHttp, parm);
		}
	}
//	alert(sr);
	xmlHttp.send(sr);
	if (!async)
		return XMLReq._onSendSoapRequest(method, async, callback, xmlHttp, parm);
}

XMLReq._onSendSoapRequest = function(method, async, callback, req, parm) 
{
    //alert("Response:" + req.responseText);
	if(callback)
		callback(req.responseXML, parm);
	if(!async)
		return req.responseXML;
}


// private: xmlhttp factory
XMLReq._getXmlHttp = function() 
{
	try
	{
		if(window.XMLHttpRequest) 
		{
			var req = new XMLHttpRequest();
			// some versions of Moz do not support the readyState property and the onreadystate event so we patch it!
			if(req.readyState == null) 
			{
				req.readyState = 1;
				req.addEventListener("load", 
									function() 
									{
										req.readyState = 4;
										if(typeof req.onreadystatechange == "function")
											req.onreadystatechange();
									},
									false);
			}
			return req;
		}
		if(window.ActiveXObject) 
			return new ActiveXObject(XMLReq._getXmlHttpProgID());
	}
	catch (ex) {}
	throw new Error("Your browser does not support XmlHttp objects");
}
XMLReq._getXmlHttpProgID = function()
{
	if(XMLReq._getXmlHttpProgID.progid)
		return XMLReq._getXmlHttpProgID.progid;
	var progids = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
	var o;
	for(var i = 0; i < progids.length; i++)
	{
		try
		{
			o = new ActiveXObject(progids[i]);
			return XMLReq._getXmlHttpProgID.progid = progids[i];
		}
		catch (ex) {};
	}
	throw new Error("Could not find an installed XML parser");
}




// Objects for parsing xml results from service.
function XMLRequestParser(doc)
{
    this.fieldlist = doc.getElementsByTagName("field");
    this.currfield = this.fieldlist;
    this.errorlist = doc.getElementsByTagName("error");
    this.error = false;
    if (this.errorlist.length > 0)
        this.error = true;
}

XMLRequestParser.prototype.CheckError = function()
{
    return this.error;
}

XMLRequestParser.prototype.GetError = function()
{
    return this.errorlist[0].firstChild.nodeValue;
}

XMLRequestParser.prototype.GetField = function(id)
{
    for (var i=0;i<this.fieldlist.length; i++) {
        if (this.fieldlist[i].attributes.length > 0 && this.fieldlist[i].attributes[0].name == "id" && this.fieldlist[i].attributes[0].value == id)
        {
        if (this.fieldlist[i].firstChild)
            return unescape(this.fieldlist[i].firstChild.nodeValue);
        return "";
        }
    }
    return null;
}
